package com.tbit.uqbike.webmanager.util;

import cn.hutool.core.util.NumberUtil;
import com.tbit.utils.DateTimeUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

@Slf4j
public class NumberUtils {
    static DecimalFormat df = new DecimalFormat("#.00");

    static {
        df.setMaximumFractionDigits(2);
        df.setGroupingSize(0);
        df.setRoundingMode(RoundingMode.FLOOR);
    }

    /**
     * 格式化(不需要四舍五入)
     *
     * @param value
     * @return float
     */
    public static String round(Number value) {
        return df.format(value);
    }

    /**
     * 获取指定位数随机字符串
     *
     * @param length
     * @return
     */
    public static String buildRandom(int length) {
        String str = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        @SuppressWarnings("rawtypes")
        List list = new ArrayList();
        while (sb.length() < length) {
            int number = random.nextInt(10);
            if (sb.length() > 0 && !"0".equals(sb.substring(0, 1))) {
                int n = 0;
                for (int i = 0; i < list.size(); i++) {
                    if (!((Object) number).equals(list.get(i))) {
                        n++;
                    }
                }
                if (n == sb.length()) {
                    sb.append(str.charAt(number));
                    list.add(number);
                }
            } else {
                if (!"0".equals(String.valueOf(str.charAt(number)))
                        && sb.length() == 0) {
                    sb.append(str.charAt(number));
                    list.add(number);
                }
            }
        }

        return sb.toString();
    }

    public static void main(String[] args) throws IOException {

        File file = new File("D:/QRcode/1.txt");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
        OutputStream outputStream = null;
        try {
            List<String> list = new LinkedList<>();
            for (int i = 0; i < 80; i++) {
                String num = DateTimeUtil.getYYYYMMDDHHSS() + NumberUtils.buildRandom(4);
                while (list.contains(num)) {
                    num = DateTimeUtil.getYYYYMMDDHHSS() + NumberUtils.buildRandom(4);
                }
                System.out.println(num);
                list.add(num);
                outputStream = new FileOutputStream(file, true);
                outputStream.write(num.getBytes(Charset.forName("UTF-8")));
                outputStream.write("\r\n".getBytes(Charset.forName("UTF-8")));
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            outputStream.close();
        }

    }

    /**
     * 加法
     *
     * @param values
     * @return
     */
    public static BigDecimal add(Number... values) {
        return NumberUtil.add(values);
    }

    /**
     * 减法
     *
     * @param v1
     * @param v2
     * @return
     */
    public static BigDecimal sub(Number v1, Number v2) {
        return NumberUtil.sub(v1, v2);
    }

}
